package nx.simple3d.data;

import java.util.ArrayList;
import java.util.List;

public class TriangleBase{
  protected int[] a;
  protected int[] b;
  protected int[] c;
  
  private List<TriangleHandler> triangleHandlers;
  private static TriangleBase instance;
  
  private TriangleBase() {}
  
  public static TriangleBase getInstance(){
    if(instance == null){
      instance = new TriangleBase();
    }
    return instance;
  }
  
  private List<TriangleHandler> getTriangleHandlers(){
    if(triangleHandlers == null){
      triangleHandlers = new ArrayList<TriangleHandler>();
    }
    return triangleHandlers;
  }
  
  public void compile(){
    int size = 0;
    
    for(TriangleHandler th : getTriangleHandlers()){
      th.setTriangleOffset(size);
      size += th.getRequiredTriangleCacheSize();
    }
    
    a = new int[size];
    b = new int[size];
    c = new int[size];
    
    for(TriangleHandler th : getTriangleHandlers())
      th.initializeTriangleData();
  }
  
  public void setTriangle(int index, int a, int b, int c){
    this.a[index] = a;
    this.b[index] = b;
    this.c[index] = c;
  }
  
  public void registerTriangleHandler(TriangleHandler th){
    getTriangleHandlers().add(th);
  }
}
